package com.xyp.homework;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/8/5
 * @Desc 从想做到能做
 */
class User {
    private String name;
    private Integer times;

    public User(String name, Integer times) {
        this.name = name;
        this.times = times;
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getTimes() {
        return times;
    }

    public void setTimes(Integer times) {
        this.times = times;
    }
}

class Account {
    private double balance;

    public Account() {
    }

    public Account(double balance) {
        this.balance = balance;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

}

class Atm implements Runnable {
    private User user;
    private Account account;
    private Double money;

    public Atm() {
    }

    public Atm(User user, Account account, Double money) {
        this.user = user;
        this.account = account;
        this.money = money;
    }

    public Atm(Account account, Double money) {
        this.account = account;
        this.money = money;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (account) {
                if (user.getTimes() <= 0) {
                    break;
                } else {
                    user.setTimes(user.getTimes()-1);
                    double balance = account.getBalance() + money;
                    account.setBalance(balance);
                    System.out.println(user.getName() + "存了" + money + "元，目前余额：" + balance);
                }
            }
        }
    }
}


public class Demo {
    public static void main(String[] args) {

        User u1 = new User("张三",3);
        User u2 = new User("李四",3);

        Account account = new Account();

        Atm atm1 = new Atm(u1,account,1000.0);
        Atm atm2 = new Atm(u2,account,1000.0);

        new Thread(atm1).start();
        new Thread(atm2).start();
    }
}
